﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using System.ComponentModel;
using System.IO.Ports;

namespace Argos.Activities.MessageDelivery
{
	public sealed class SerialPortSMSDeliveryActivity : MessageDeliveryActivityBase
	{
		public SerialPortSMSDeliveryActivity()
		{
			this.BaudRate = 9600;
			this.Parity = Parity.None;
			this.DataBits = 8;
			this.StopBits = StopBits.One;
		}

		#region public string PortName {get; set; }

		public static readonly DependencyProperty PortNameProperty = DependencyProperty.Register("PortName", typeof(string), typeof(SerialPortSMSDeliveryActivity));

		[Category("Serial port")]
		[Description("Specifies the name of the serial port which the GSM/GPRS modem is connected to.")]
		public string PortName
		{
			get
			{
				return (string)this.GetValue(PortNameProperty);
			}
			set
			{
				this.SetValue(PortNameProperty, value);
			}
		}

		#endregion

		#region public string PIN {get; set;}

		[Category("Serial port")]
		[Description("Speficies the PIN code of the SIM card which is inserted into the GSM/GPRS modem.")]
		public string PIN
		{
			get { return (string)GetValue(PINProperty); }
			set { SetValue(PINProperty, value); }
		}

		public static readonly DependencyProperty PINProperty = DependencyProperty.Register("PIN", typeof(string), typeof(SerialPortSMSDeliveryActivity));

		#endregion



		#region public int BaudRate {get; set;}
		
		[Category("Serial port")]
		[Description("Specifies the baud rate of the serial port which the GSM/GPRS modem is connected to.")]
		[DisplayName("Baud Rate")]
		[DefaultValue(9600)]
		public int BaudRate
		{
			get { return (int)GetValue(BaudRateProperty); }
			set { SetValue(BaudRateProperty, value); }
		}

		public static readonly DependencyProperty BaudRateProperty = DependencyProperty.Register("BaudRate", typeof(int), typeof(SerialPortSMSDeliveryActivity));

		#endregion



		#region public Parity Parity {get; set;}
		[Category("Serial port")]
		[Description("Specifies the parity of the serial port which the GSM/GPRS modem is connected to.")]
		[DefaultValue(Parity.None)]
		public Parity Parity
		{
			get { return (Parity)GetValue(ParityProperty); }
			set { SetValue(ParityProperty, value); }
		}

		public static readonly DependencyProperty ParityProperty = DependencyProperty.Register("Parity", typeof(Parity), typeof(SerialPortSMSDeliveryActivity));

		#endregion

		#region public int DataBits {get; set;}
		[Category("Serial port")]
		[Description("Specifies the data bits of the serial port which the GSM/GPRS modem is connected to.")]
		[DisplayName("Data Bits")]
		[DefaultValue(8)]
		public int DataBits
		{
			get { return (int)GetValue(DataBitsProperty); }
			set { SetValue(DataBitsProperty, value); }
		}

		public static readonly DependencyProperty DataBitsProperty = DependencyProperty.Register("DataBits", typeof(int), typeof(SerialPortSMSDeliveryActivity));
		
		#endregion

		#region public StopBits StopBits {get; set}
		[Category("Serial port")]
		[Description("Specifies the stop bits of the serial port which the GSM/GPRS modem is connected to.")]
		[DisplayName("Stop Bits")]
		[DefaultValue(StopBits.One)]
		public StopBits StopBits
		{
			get { return (StopBits)GetValue(StopBitsProperty); }
			set { SetValue(StopBitsProperty, value); }
		}

		public static readonly DependencyProperty StopBitsProperty = DependencyProperty.Register("StopBits", typeof(StopBits), typeof(SerialPortSMSDeliveryActivity));

		#endregion


		protected override System.Workflow.ComponentModel.ActivityExecutionStatus Execute(System.Workflow.ComponentModel.ActivityExecutionContext executionContext)
		{
			SMSDispatcher dispatcher = new SMSDispatcher(this.PIN, this.PortName);
			dispatcher.SendSMS(Message.RecipientAddress, Message.Body);
			
			return System.Workflow.ComponentModel.ActivityExecutionStatus.Closed;
		}
	}
}
